// pages/staff/newmyyeji.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        count_num:0, //订单总条数
        pay_methods: [],  //缴费方式
        pay_method_index: 0,  //缴费方式下标
        pay_method_id:'',
        otoType: [],
        otoType_index: 0,
        otoType_id:0,
        teamtype:1,  //2团队中课程顾问 3总的团队业绩
        topindex: 0,
        everyarr: ['今日业绩', '本周业绩', '本月业绩'],
        everytitle:'本月业绩',
        wxpayment: [{ title: '请选择付款方式', type: 0 }, { title: '微信', type: 1 }, { title: '支付截图', type: 2 }, { title: '余额支付', type: 3 }, { title: 'iOS内购支付', type: 4 }, { title: '支付宝', type: 5 }, { title: '内容市场开课', type: 6 }, { title: '京东', type: 7 }, { title: '花呗分期', type: 8 }, { title: '白条分期', type: 9 }, { title: '其他', type: 10 }],
        wxpay_index: 0,
        wxpay_id:'',
        indexarr: 2,
        showernon: false,
        datestart: '开始时间',
        dateend: '结束时间',
        lists: [],
        amount: 0,
        code: '',
        msg: '',
        consultant_id: '',
        limit:10,
        page:0,
        keyword:'',
        billobj:{},
        is_auto:'',
        is_team: 0,
        is_depart: 0,
        teamArray: [],
        teamregin: ['', ''],//课程
        teamIndex: 0,//课程下标
        team_id:'',
        team_title: '全部部门',
        team_title1: '全部',
        team_title2: '全部',
        classarr: [],  //校区
        classarr_index: 0,  //校区下标
        classarr_id: '',  //校区id
        classpart: [],  //校区下的部门
        classpart_index: 0,  //校区下的部门小标
        classpart_id: '',  //校区下的部门id
        showerfa:false,
        indicatorDots: true,
        interval: 5000,
        duration: 1000,
        imgUrl: CONFIG.API_PIC,
        jietu:[],
    },
    inputblur(e){
        var that = this;
        // console.log(e);
        that.setData({
            keyword: e.detail.value
        })
    },
    paymentChange(e){
        var that = this;
        var index = e.detail.value;
        that.setData({
            wxpay_index: index,
            wxpay_id: that.data.wxpayment[index].type,
        })
        // console.log(index);
    },
    seacrchclick(e){
        // console.log(e);
        var that = this;
        var phone = e.detail.value.phone;
        that.setData({
            keyword:phone,
            page:0
        })
        that.yejiLoadMore()
    },
    closeclick(){
        var that = this;
        that.setData({
            picKan: false,
        })
    },
    paychart(e){
        var that = this;
        // console.log(e);
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var newjietu = e.target.dataset.item.chart;
        var srr = newjietu.split(',');
        wx.hideLoading();
        if (Number(srr.length) > 0) {
            that.setData({
                picKan: true,
                jietu: srr
            })
        } else {
            wx.showToast({
                title: '暂无凭证',
                icon: "none",
                duration: 1500
            })
        }
        // var jiaoyanCode = CONFIG.API_PIC + item.chart;
        // // var jiaoyanCode = list[index].qr_code;
        // that.setData({
        //     jietu: jiaoyanCode,
        //     showerca: true
        // })
        
    },
    // 点击手动入账
    shotype(){
        var that = this;
        var is_auto = that.data.is_auto;
        if (Number(is_auto)==2){
            that.setData({
                is_auto: ''
            })
        }else{
            that.setData({
                is_auto:2
            })
        }
    },
    otoChange(e){
        var that = this;
        var index = e.detail.value;
        that.setData({
            otoType_index: index,
            otoType_id: that.data.otoType[index].id,
            pay_method_index:0,
            pay_method_id:'',
        })
    },
    payChange(e){
        var that = this;
        var index = e.detail.value;
        that.setData({
            pay_method_index: index,
            pay_method_id: that.data.pay_methods[index].id
        })
    },
    // 付款方式
    otoTypepay(){
        var that = this;
        share.wxRequest('crmapi/user/otoType', {}, function (res) {
            if(Number(res.data.errcode)==0){
                var lists = res.data.data;
                var obj = [{ oto_name: "全部",id:'' }];
                that.setData({
                    otoType:obj.concat(lists)
                })
                // console.log(that.data.otoType)
                that.payMethod();
            }
        })
    },
    // 缴费方式
    payMethod() {
        var that = this;
        share.wxRequest('crmapi/user/payMethod', {}, function (res) {
            if (Number(res.data.errcode) == 0) {
                var lists = res.data.data;
                var obj = [{ title: "全部", id: '' }];
                that.setData({
                    pay_methods: obj.concat(lists)
                })
            }
        })
    },
    copetypa1(e){
        var copy = e.currentTarget.dataset.phone;
        wx.setClipboardData({
            data: copy,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none',
                    duration: 1500
                })
            },
            fail: function (res) {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                    duration: 1500
                })
            },
        })
    },
    // 点击复制
    copetypa(e){
        var copy = e.currentTarget.dataset.order;
        wx.setClipboardData({
            data: copy,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none',
                    duration: 1500
                })
            }, 
            fail: function (res) {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                    duration: 1500
                })
            },
        })
    },

    
    // 点击确定
    sureclick(e){
        var that = this;
        
        var title1 = that.data.everyarr;
        var indexarr = that.data.indexarr;
        var dateend = that.data.dateend;
        var datestart = that.data.datestart;
        if (indexarr == -1 || indexarr == '-1'){
            if (dateend =='结束时间'){
                // var title = datestart +'~至今';
                wx.showToast({
                    title: '请选择结束时间',
                    icon: 'none',
                    duration: 1500
                })
            }else{
                var title = datestart + '~' + dateend;
                that.tit(title);
            } 
        }else{
            var title = title1[indexarr];
            that.tit(title);
        }
    },

    tit(title){
        var that = this;
        that.setData({
            everytitle: title,
            showernon: false,
            lists: [],
            amount: 0,
            page: 0,
            msg: '',
            showerfa:true,
        })
        that.yejiLoadMore()
    },

    // 点击重置
    nosureclick(){
        var that = this;
        that.setData({
            indexarr: 2,
            everytitle: '本月业绩',
            datestart: '开始时间',
            dateend: '结束时间',
            showernon: false,
            lists:[],
            amount: 0,
            msg:'',
            page:0,
            keyword:'',
            is_auto:'',
            pay_method_index:0,
            wxpay_index: 0,
            wxpay_id: '',
            otoType_index:0,
            otoType_id:'',
            team_id:'',
            teamregin: ['', ''],//课程
            teamIndex:0,
            // teamIndex: [0, 0],//课程下标
            team_title: '全部部门',
            team_title1: '全部',
            team_title2: '全部',
            classarr_index: 0,  //校区下标
            classarr_id: '',  //校区id
            classpart_index: 0,  //校区下的部门小标
            classpart_id: '',  //校区下的部门id
            showerfa: false,
        })
        that.yejiLoadMore()
    },


    // 选择业绩
    chooseyeji(e) {
        var that = this;
        var index = e.target.dataset.index;
        that.setData({
            indexarr: index,
            datestart: '开始时间',
            dateend: '结束时间',
        })
    },



    // 点击空白
    showernon() {
        var that = this;
        that.setData({
            showernon: false
        })
    },


    // 点击今日业绩
    yejinon() {
        var that = this;
        that.setData({
            showernon: true,
        })
    },

    // 切换title
    titleclick(e) {
        var that = this;
        var index = e.target.dataset.index;
        that.setData({
            topindex: index,
            otoType_id:0,
            pay_method_id:0,
            otoType_index:0,
            pay_method_index:0,
            showerfa:false,
            indexarr: 2,
            datestart: '开始时间',
            dateend: '结束时间',
            lists:[],
            everytitle: '本月业绩',
            amount:0,
            page: 0,
            keyword:'',
        })
        that.billSales();
        that.yejiLoadMore();
    },

    //  选择起始时间
    bindDateChangestart: function (e) {
        this.setData({
            datestart: e.detail.value,
            indexarr:-1,
        })
    },

    // 结束时间
    bindDateChangeend: function (e) {
        
        // console.log(this.data.datestart == '起始时间');
        if (this.data.datestart == '开始时间') {
            wx.showToast({
                title: '请先选择开始时间',
                icon: 'none',
                duration: 1500
            })
        } else {
            // console.log(e);
            this.setData({
                dateend: e.detail.value,
                indexarr: -1,
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载 
     */
    onLoad: function (options) {
        var that = this;
        if (Number(options.teamtype) == 2){
            var consultant_id = options.consultant_id;
            wx.setNavigationBarTitle({
                title: '业绩',
            })
        } else if (Number(options.teamtype) == 3){
            var consultant_id = '';
            wx.setNavigationBarTitle({
                title: '团队业绩',
            })
        }else{
            var consultant_id = app.globalData.admin_id;
            // var consultant_id = 607;
            wx.setNavigationBarTitle({
                title: '我的业绩',
            })
        }
        var role = app.globalData.role;
        // var is_team = role.is_team;
        // var is_depart = role.is_depart;
        // var is_team = 1;
        // var is_depart = 0;
        // that.setData({
        //     is_team: is_team,
        //     // is_depart: is_depart
        // })
        // if (Number(is_team) == 1) {
            // 获取校区
            that.allteams();
        // } else {
        //     // that.depart(1);
        // }
        that.setData({
            consultant_id: consultant_id,
            teamtype: options.teamtype
            // consultant_id:'',
            // teamtype:3
        })
        that.yejiLoadMore();
        that.billSales();
        that.otoTypepay();
    },
    classChange(e){
        var that = this;
        var index = e.detail.value;
        that.setData({
            teamIndex: index,
            team_id: that.data.teamArray[index].id,
            team_title1: that.data.teamArray[index].title,
        })
    },
    teamChange(e) {
        var that = this;
        // console.log(899)
        var firstid = e.detail.value[0];
        var secid = e.detail.value[1];
        // console.log(firstid)
        // console.log(secid)
        var firsttitle = this.data.teamArray[0][firstid];
        var sectitle = this.data.teamArray[1][secid];
        var first_id = this.data.classarr[firstid].id;
        var sec_id = this.data.classpart[secid].id;
        if (sectitle == '全部部门') {
            var team_title = firsttitle;
            var team_title2 = '';
        } else {
            var team_title = firsttitle + '_' + sectitle;
            var team_title2 = sectitle;
        }
        that.setData({
            teamregin: [firsttitle, sectitle],
            "teamIndex[0]": e.detail.value[0],
            "teamIndex[1]": e.detail.value[1],
            team_title: team_title,
            team_title1: firsttitle,
            team_title2: team_title2,
            classarr_id: first_id,
            classpart_id: sec_id,
        })
        that.setData({
            page: 0
        })
        // that.team();

    },
    // 选择课程
    teamPickerChange: function (e) {
        // vm = this;
        var that = this;
        // console.log(899)
        switch (e.detail.column) {
            case 0:
                var list = [];
                // console.log(e.detail.value)
                if (Number(e.detail.value) > 0) {
                    var objarr = that.data.classarr[e.detail.value];
                    var id = objarr.id;
                    that.setData({
                        classarr_id: id,
                        classarr_index: e.detail.value,
                    })
                    that.depart(2);
                    that.setData({
                        "teamIndex[0]": e.detail.value,
                    })
                } else {
                    list.push('全部部门')
                    that.setData({
                        "teamArray[1]": list,
                        "teamIndex[0]": e.detail.value,
                        "teamIndex[1]": 0,
                    })
                }

        }
    },
    // 获取校区
    allteams() {
        var that = this;
        var obj = {
            phone: wx.getStorageSync('tel')
        }
        share.wxRequest('crmapi/Teams/allteams', obj, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                // if (Number(data.length) == 1) {
                //     var data1 = data;
                //     // console.log(data1);
                //     that.setData({
                //         classarr_id: data1[0].id
                //     })
                //     that.depart(1);
                // } else {
                var obj1 = [
                    {
                        "id": '',//校区ID
                        "type": '',
                        "title": "全部校区"//校区名称
                    }
                ]
                var data1 = obj1.concat(data);
                // var firarr = [];
                // for (var i = 0; i < data1.length; i++) {
                //     firarr.push(data1[i].title);
                // }
                // var firall = [firarr, ['全部部门']];
                // console.log(data1);
                that.setData({
                    teamArray: data1,
                })
                // }
                that.setData({
                    classarr: data1,
                    classarr_id: data1[0].id
                })
            }
        })
    },
    // 校区下的部门
    depart(type) {
        var that = this;
        var obj = {
            phone: wx.getStorageSync('tel'),
            teamId: that.data.classarr_id
        }
        share.wxRequest('crmapi/Teams/depart', obj, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj1 = [
                    {
                        "id": '',//校区ID
                        "name": "全部部门"//校区名称
                    }
                ]
                var data1 = obj1.concat(data)
                that.setData({
                    classpart: data1,
                })
                if (Number(type) == 2) {
                    var list = [];
                    for (var i = 0; i < data1.length; i++) {
                        list.push(data1[i].name)
                    }
                    that.setData({
                        "teamArray[1]": list,
                        "teamIndex[1]": 0,
                    })
                }
            }
        })
    },


    // 我的业绩 昨天 今天 本周 本月
    billSales(){
        var that = this;
        if (Number(that.data.teamtype)==3){
            var obj = {
                phone:wx.getStorageSync('tel')
            }
            if (Number(that.data.topindex)==1){
                var url = 'crmapi/Teams/teamsWkBillSales'
            }else{
                var url = 'crmapi/Teams/teamsBillSales'
            }
        }else{
            var obj = {
                consultant_id: that.data.consultant_id
            }
            if (Number(app.globalData.identity) == 3) {
                if (Number(that.data.topindex) == 1) {
                    var url = 'crmapi/Learn/wkbillSales'
                } else {
                    var url = 'crmapi/Learn/billSales'
                }
            }else{
                if (Number(that.data.topindex) == 1) {
                    var url = 'crmapi/order/wkbillSales'
                } else {
                    var url = 'crmapi/order/billSales'
                }
            }
            
        }
        share.wxRequest(url, obj, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                that.setData({
                    billobj:data
                })
            }
        })
    },

    
    yejiLoadMore: function () {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        let that = this;
        var consultant_id = that.data.consultant_id;
        var target = Number(that.data.indexarr);
        var datestart = that.data.datestart;
        var dateend = that.data.dateend;
        var page = that.data.page;
        var limit = that.data.limit;
        var phonename = that.data.keyword;
        if (Number(target)==-1){
            if (dateend == '结束时间'){
                var dateend = '';
            }else{
                var dateend = dateend;
            }
        }else{
            var dateend = '';
            var datestart = '';
        }
        if (Number(that.data.teamtype) == 3) {
            if (Number(that.data.otoType_id)==1){
                var pay_method = that.data.pay_method_id
            }else{
                var pay_method = ''
            }
            var data = {
                user_phone: phonename,
                phone: wx.getStorageSync('tel'),
                start_date: datestart,
                end_date: dateend,
                page: page,
                oto_type: that.data.otoType_id,
                pay_method: pay_method,
                is_auto: that.data.is_auto,
                teamId: that.data.team_id,  //否	int	校区ID
                payment: that.data.wxpay_id,
                // departId: that.data.classpart_id,   //	否	int	部门ID
            }
            if (Number(that.data.topindex)==1){
                var url = 'crmapi/Teams/teamsWkBill'
            }else{
                var url = 'crmapi/Teams/teamsBill'
            }
        } else {
            
            if (Number(that.data.topindex) == 1) {
                var data = {
                    user_phone: phonename,
                    consultant_id: consultant_id,
                    start_date: datestart,
                    end_date: dateend,
                    page: page,
                    payment: that.data.wxpay_id,
                }
                if (Number(app.globalData.identity) == 3) {
                    var url = 'crmapi/Learn/wkbill'
                }else{
                    var url = 'crmapi/order/wkbill'
                }
            } else {
                if (Number(that.data.otoType_id) == 1) {
                    var pay_method = that.data.pay_method_id
                } else {
                    var pay_method = ''
                }
                var data = {
                    user_phone: phonename,
                    consultant_id: consultant_id,
                    start_date: datestart,
                    end_date: dateend,
                    page: page,
                    oto_type: that.data.otoType_id,
                    pay_method: pay_method,
                    is_auto: that.data.is_auto
                }
                if (Number(app.globalData.identity) == 3) {
                    var url = 'crmapi/Learn/bill'
                }else{
                    var url = 'crmapi/order/bill'
                }
            }
        }
        
        var index = that.data.topindex;
        share.wxRequest(url, data, function (res) {
            if (Number(res.data.errcode) == 0){
                var data = res.data.data;
                if(data.lists){
                    var newslist = data.lists;
                }else{
                    var newslist = [];
                }
                if(Number(page)==0){
                    var lists = newslist;
                }else{
                    var lists = that.data.lists.concat(newslist);
                }

                if(Number(lists.length)!=0){
                    var msg = res.data.msg;
                    if (res.data.count_num){
                        that.setData({
                            count_num: res.data.count_num
                        })
                    }
                }else{
                    var msg = '还没有业绩'
                    that.setData({
                        count_num:0
                    })
                }
                
                that.setData({
                    code: res.data.errcode,
                    msg: msg,
                    lists: lists,
                    amount: data.countAmount,
                    // allcount:
                })
            }else{
                that.setData({
                    code: res.data.errcode,
                    msg: res.data.msg,
                    lists: that.data.lists,
                    amount: that.data.amount,
                })
            }
            wx.hideLoading();
        })
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        // console.log(8989);
        wx.showLoading({
            title: '加载中~',
        })
        
        var page = Number(that.data.page)+1;
        that.setData({
            page:page
        })
        that.yejiLoadMore();
        
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },



})